package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type FileInfo struct {
	Id int64
	Filename string
	Filetype string
	Filepath  string
	Deviceid string
	Company  string
	Coordinate string
	Timestamp int64
	Uploadtime string
}


//添加文件
func AddFile(file_info *FileInfo)(int64,error){
	db := orm.NewOrm()
	id,err := db.Insert(file_info)
	return id,err
}

//加载所有文件
func ReadFile(fileDataChan chan string,company string,page string, size string) int {
	var filerows []FileInfo
	o := orm.NewOrm()
	pageint, _ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	var totalItem int = 0
	o.Raw("SELECT COUNT(*) FROM file_info where company = ? ", company).QueryRow(&totalItem)
	//fmt.Println(totalItem)
	if _, err := o.Raw("SELECT * FROM file_info where company = ?  ORDER BY uploadtime DESC limit ?,? ", company, (pageint-1)*sizeint, sizeint).QueryRows(&filerows); err != nil {
		fmt.Println("err")
		return 0
	}

	var  i int64= (pageint-1)*sizeint;
	for _,filerow := range filerows{
		i++
		id := strconv.FormatInt(i,10)
		fileInfos :=id+ "and"+ filerow.Filename+
			"and"+filerow.Filetype+
		//"and"+fencerow.Fencepoint+
			"and"+filerow.Deviceid+
			"and"+filerow.Company+
			"and"+filerow.Coordinate+
			"and"+filerow.Uploadtime
		fileDataChan <- fileInfos
	}
	return totalItem
}


//文件筛选
func Queryfilebycondition(fileDataChan chan string,company string,starttime string, endtime string,filetype string, deviceid string,page string, size string) int {
	var filerows []FileInfo
	o := orm.NewOrm()
	pageint ,_ := strconv.ParseInt(page, 10, 64)
	sizeint, _ := strconv.ParseInt(size, 10, 64)
	var totalItem int = 0
	   if deviceid == "notdeviceid"{//条件筛选
		   o.Raw("SELECT COUNT(*) FROM file_info WHERE company = ? AND filetype = ?  AND uploadtime BETWEEN ? AND ?", company, filetype, starttime, endtime).QueryRow(&totalItem)
		   //fmt.Println(totalItem)
		   if _, err := o.Raw("SELECT * FROM file_info WHERE company = ? AND filetype = ?  AND  uploadtime BETWEEN ? AND ? ORDER BY uploadtime DESC limit ?,? ", company, filetype, starttime, endtime, (pageint-1)*sizeint, sizeint).QueryRows(&filerows); err != nil {
			   fmt.Println("err")
			   return 0
		   }
	   }else { //根据deviceid搜索
	       //fmt.Println("=============ssssssssssssssss================")
		   o.Raw("SELECT COUNT(*) FROM file_info WHERE company = ? and deviceid = ?", company,deviceid).QueryRow(&totalItem)
		   //fmt.Println(totalItem)
		   if _, err := o.Raw("SELECT * FROM file_info WHERE company = ? and deviceid = ?  ORDER BY uploadtime DESC limit ?,? ", company,deviceid,(pageint-1)*sizeint, sizeint).QueryRows(&filerows); err != nil {
			   fmt.Println("err")
			   return 0
		   }
	   }
	//fmt.Println("n =",n)
	var  i int64= (pageint-1)*sizeint;
	for _,filerow := range filerows{
		i++
		id := strconv.FormatInt(i,10)
		alarmInfos :=id+ "and"+ filerow.Filename+
			"and"+filerow.Filetype+
			"and"+filerow.Deviceid+
			"and"+filerow.Company+
			"and"+filerow.Coordinate+
			"and"+filerow.Uploadtime
		fileDataChan <- alarmInfos
	}
	return totalItem
}
